
from flask import Flask, render_template, request, redirect,session
from flask_cors import  CORS
from json import *
app = Flask(__name__)
cors = CORS(app,resources={r"/*":{"origin":"*"},"r/webinit":{"origin":"*"}})
MsgList={'温度':[123,123,12323,23],'湿度':[1231,123,22,3,4],'帅气值':[100,100,100]}
Init={'姓名':"丁天威",'学号':"1850700",'项目':"温度测量"}  #name id project
@app.route('/',methods=['GET','POST'])
def hello_world():
    if request.method == "POST":
        print(request.url)  # 请求地址
        print(request.url_charset) # URL 编码方式
        print(request.url_root) # 请求地址 完整请求地址 host
        print(request.url_rule) # 请求路由地址
        print(request.values.to_dict())  # 接收所有(GET,POST)请求中的数据,包含了 URL 和 FormData 中的数据
        print(request.args.get("id"))  # 获取URL中的数据 字符串

    return render_template('index.html')
@app.route('/init',methods=['GET','POST'])

def init():
    if request.method == "POST":
        getJson = loads(request.values)
    Init=getJson
@app.route('/webinit',methods=['GET','POST'])
def webinit():
    return dumps(Init)

@app.route('/getData',methods=['GET','POST'])
def getData():
    if request.method == "POST":
        getJson=loads(request.values)
    if  getJson['name'] not in MsgList.keys():
        MsgList[getJson['name']]=getJson['data']
    else:
        MsgList[getJson['name']].append(getJson[getJson['name']])
    for i in MsgList.keys():
        if len(MsgList[i])>10:
            while len(MsgList[i])>10:
                MsgList[i].pop(0)
    return 'OK'
@app.route('/getMsg',methods=['GET','POST'])
def getMsg():
    retDic = {}
    for i in MsgList.keys():
        retDic[i] = MsgList[i][-1]
    jsonmsg = dumps(retDic)
    return jsonmsg

if __name__ == '__main__':
    app.run(app,debug=True)
